package com.example.keli.ui.userinfo;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.keli.R;
import com.example.keli.base.App;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.utils.DialogUtil;
import com.example.keli.base.utils.KeyBordUtil;
import com.example.keli.base.utils.LogUtils;
import com.example.keli.base.utils.SPUtils;
import com.example.keli.base.utils.ToastUtil;
import com.flyco.roundview.RoundTextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * description: 选择兴趣爱好
 * author: Allen
 * date: 2019/3/25 15:13
 */
public class SelectHobbyActivity extends BaseActivity {


    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.save)
    RoundTextView mSave;
    List<String> list = new ArrayList<>();
    List<String> inList = new ArrayList<>();
    List<HobbyData> hobbyDatas = new ArrayList<>();
    @BindView(R.id.diy_hobby)
    EditText diyHobby;
    @BindView(R.id.add_diy)
    RoundTextView addDiy;
    @BindView(R.id.diy_hobby_layout)
    LinearLayout diyHobbyLayout;
    HobbyAdapter adapter;
    String hobbys = "";

    public static void start(Activity context, String hobby) {
        Intent intent = new Intent(context, SelectHobbyActivity.class);
        intent.putExtra("hobby", hobby);
        context.startActivityForResult(intent, 1002);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_select_hobby;
    }

    @Override
    protected void bindData() {
        abTitle.setText("兴趣爱好");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        list.addAll( Arrays.asList(getResources().getStringArray(R.array.hobby_list)));
        hobbys = getIntent().getStringExtra("hobby");
        inList = Arrays.asList(hobbys.split(","));
        //本地爱好
        String localHobby = getHobby();
        List<String> localList = Arrays.asList(localHobby.split(","));
        LogUtils.loge("看下本地有几个>>>"+localList.size()+"这是什么》"+localList.get(0));
        if(localList.size()==1){
            if(TextUtils.isEmpty(localList.get(0))){
                localList = new ArrayList<>();
            }
        }
        list.addAll(localList);
        for (String s : list) {
            HobbyData hobbyData = new HobbyData();
            if (inList.contains(s)) {
                hobbyData.setCheck(true);
            } else {
                hobbyData.setCheck(false);
            }
            hobbyData.setHobby(s);
            hobbyDatas.add(hobbyData);
        }
        for (String s : inList) {
            if(TextUtils.isEmpty(s)){
                continue;
            }
            if (!list.contains(s)) {
                //不包含，说明是自定义的
                HobbyData hobbyData = new HobbyData();
                hobbyData.setCheck(true);
                hobbyData.setHobby(s);
                hobbyDatas.add(hobbyData);
            }
        }

        adapter = new HobbyAdapter(hobbyDatas);
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                setResult(1003,getIntent().putExtra("hobby",hobbyDatas.get(position).getHobby()));
//                finish();
                boolean check = hobbyDatas.get(position).isCheck();
                hobbyDatas.get(position).setCheck(!check);
                adapter.notifyDataSetChanged();
            }
        });

        View headerView = getLayoutInflater().inflate(R.layout.header_hobby, null);
        headerView.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加自定义
                diyHobbyLayout.setVisibility(View.VISIBLE);
                diyHobby.requestFocus();
                diyHobby.setFocusable(true);
                KeyBordUtil.showSoftKeyboard(diyHobby);
//                showDialog();
            }
        });
        adapter.addFooterView(headerView);
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (diyHobby.isShown()) {
                    diyHobbyLayout.setVisibility(View.GONE);
                }
                return false;
            }
        });
    }

    StringBuilder outHobbys = new StringBuilder();

    @OnClick({R.id.save, R.id.add_diy, R.id.recycler_view})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.save:
                for (int i=0; i<hobbyDatas.size(); i++){
                    HobbyData hobbyData = hobbyDatas.get(i);
                    if (hobbyData.isCheck() && i<hobbyDatas.size()){
                        outHobbys = outHobbys.append(hobbyData.getHobby());
                        if (i<hobbyDatas.size()){
                            outHobbys.append(",");
                        }
                    }
                }
                setResult(1003, getIntent().putExtra("hobby", outHobbys.toString()));
                finish();
                break;
            case R.id.add_diy:
                if (diyHobby.getText().toString().length() > 5) {
                    ToastUtil.show("标签内容最对5个字符");
                    return;
                }
                if (panduan(diyHobby.getText().toString())) {
                    ToastUtil.show("已经有此爱好了");
                    return;
                }
                HobbyData hobbyData = new HobbyData();
                hobbyData.setCheck(false);
                hobbyData.setHobby(diyHobby.getText().toString());
                hobbyDatas.add(hobbyData);
                adapter.notifyDataSetChanged();
                putHobby(diyHobby.getText().toString());
                diyHobby.setText("");
                break;
            case R.id.recycler_view:
                diyHobbyLayout.setVisibility(View.GONE);
                break;
        }
    }

    private boolean panduan(String hobby) {
        for (HobbyData hobbyData : hobbyDatas) {
            if (TextUtils.equals(hobbyData.getHobby(), hobby)) {
                return true;
            }
        }
        return false;
    }

    public class HobbyAdapter extends BaseQuickAdapter<HobbyData, BaseViewHolder> {
        public HobbyAdapter(List<HobbyData> mData) {
            super(R.layout.item_hobby, mData);
        }

        @Override
        protected void convert(BaseViewHolder helper, HobbyData hobbyData) {
            TextView roundTextView = helper.getView(R.id.roundtext);
            if (hobbyData.isCheck()) {
                //选中的
                roundTextView.setSelected(true);
            } else {
                roundTextView.setSelected(false);
            }
            roundTextView.setText(hobbyData.getHobby());
        }
    }

    private void showDialog() {
        View view = getLayoutInflater().inflate(R.layout.hobby_dialog, null);
        DialogUtil.getMenuDialog(SelectHobbyActivity.this, view).show();
    }

    private void putHobby(String hobby) {
        String hobbyString = SPUtils.get(SelectHobbyActivity.this, App.getUid() + "hobby", "");
        hobbyString+=(hobby+",");
        SPUtils.put(getActivity(),App.getUid()+"hobby",hobbyString);
    }
    private String getHobby() {
        return SPUtils.get(SelectHobbyActivity.this, App.getUid() + "hobby", "");
    }
}
